module ctrl (
    input clk,
    input rst_n,
    input [15:0] t_data,
    output [23:0]  dis_data,
    output en,
    output tx1
);

    localparam 
                ASCII0  = 8'h30,//0
                ASCII1  = 8'h31,//1
                ASCII2  = 8'h32,//2
                ASCII3  = 8'h33,//3
                ASCII4  = 8'h34,//4
                ASCII5  = 8'h35,//5
                ASCII6  = 8'h36,//6
                ASCII7  = 8'h37,//7
                ASCII8  = 8'h38,//8
                ASCII9  = 8'h39,//9
                ASCIIdot= 8'h2e,//.
                ASCIIhx = 8'h0A,// /n
                ASCIIw  = 8'hCE,//温
                ASCIIe  = 8'hC2,
                ASCIId  = 8'hB6,//度
                ASCIIu  = 8'hC8,
                ASCIImh = 8'h3A,//:
                ASCIISHE= 8'hA1,//℃
                ASCIISHI= 8'hE6,//
                ASCIIA  = 8'h41,//A
                ASCIIB  = 8'h42,//B
                ASCIIC  = 8'h43,//C
                ASCIIa  = 8'h61,//a
                ASCIIb  = 8'h62,//b
                ASCIIc  = 8'h63,//c
                ASCIIFU = 8'h2D,//-
                ASCIIZHE= 8'h2B;//+

    parameter BIT_NUM   = 5'd16;
    parameter TIME_1s   = 28'd 50_000_000;

    reg     [7:0]   tx_data         ;

    wire [23:0] data_b;
    wire [3:0] data_b001; // 小数点的后第三位
    wire [3:0] data_b01; // 小数点的后第二位
    wire [3:0] data_b1; // 小数点的后第一位
    wire [3:0] data_1; // 个位
    wire [3:0] data_10; // 十位
    wire [3:0] data_fu; // 符号位


    assign data_b = t_data[10:0] *625; // 数据放大100倍 数据乘上精度 才是温度
    assign data_b001 = data_b / 10 %10;
    assign data_b01 = data_b / 100 %10;
    assign data_b1 = data_b / 1000 %10;
    assign data_1 = data_b / 10000 %10;
    assign data_10 = data_b / 100000 %10;
    assign data_fu = t_data[15]?4'hB:4'hA; // A -- 正 B -- 负

    assign dis_data = {data_fu,data_10,data_1,data_b1,data_b01,data_b001};

    assign en = (data_1 >= 4'd5 && data_10 >= 4'd2) || ((data_1 <= 4'd3 && data_10 <= 4'd2)) ? 1'b1 : 1'b0;


    reg     [7:0]   number          ;

    reg     [5:0]   cnt_bit         ;
    wire            add_cnt_bit     ;
    wire            end_cnt_bit     ;

    assign sign = t_data[15];



    // 每隔10s 发送一次数据
    wire end_cnt_1s;
    timer_1 inst_timer_1 (
        .clk                (clk),
        .rst_n              (rst_n),
        .end_cnt_1s         (end_cnt_1s)
    );

    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            cnt_bit <= 5'd0;
        end
        else begin
            if (add_cnt_bit) begin
                if (end_cnt_bit) begin
                    cnt_bit <= 5'd0;
                end
                else begin
                    cnt_bit <= cnt_bit + 5'd1;
                end
            end
            else begin
                cnt_bit <= cnt_bit;
            end
        end
    end
    assign add_cnt_bit = end_cnt_1s&& ~end_cnt_bit;
    assign end_cnt_bit = add_cnt_bit && cnt_bit == BIT_NUM - 1;












    always @(posedge clk or negedge rst_n) begin
        if (!rst_n) begin
            number <= 5'd0;
        end
        else begin 
            case (cnt_bit)
                5'd0    : number = 8'd30;//温
                5'd1    : number = 8'd31;
                5'd2    : number = 8'd32;//度
                5'd3    : number = 8'd33;
                5'd4    : number = 8'd34;//：
                5'd5    : number = sign?8'd37:8'd38;//+\-
                5'd6    : number = data_b / 100000 %10;
                5'd7    : number = data_b / 10000 %10;
                5'd8    : number = 8'd20;//.
                5'd9    : number = data_b / 1000 %10;
                5'd10   : number = data_b / 100 %10;
                5'd11   : number = data_b / 10 %10;
                5'd12   : number = data_b / 1 %10;
                5'd13   : number = 8'd35;//℃
                5'd14   : number = 8'd36;
                5'd15   : number = 8'd21;//换行
                default: number = number;
            endcase
        end
    end

    always @(*) begin
        case (number)
            8'd0    : tx_data <= ASCII0     ;
            8'd1    : tx_data <= ASCII1     ;
            8'd2    : tx_data <= ASCII2     ;
            8'd3    : tx_data <= ASCII3     ;
            8'd4    : tx_data <= ASCII4     ;
            8'd5    : tx_data <= ASCII5     ;
            8'd6    : tx_data <= ASCII6     ;
            8'd7    : tx_data <= ASCII7     ;
            8'd8    : tx_data <= ASCII8     ;
            8'd9    : tx_data <= ASCII9     ;
            8'd20   : tx_data <= ASCIIdot   ;
            8'd21   : tx_data <= ASCIIhx    ;
            8'd30   : tx_data <= ASCIIw     ;
            8'd31   : tx_data <= ASCIIe     ;
            8'd32   : tx_data <= ASCIId     ;
            8'd33   : tx_data <= ASCIIu     ;
            8'd34   : tx_data <= ASCIImh    ;
            8'd35   : tx_data <= ASCIISHE   ;
            8'd36   : tx_data <= ASCIISHI   ;
            8'd37   : tx_data <= ASCIIFU    ;
            8'd38   : tx_data <= ASCIIZHE   ;
            default: tx_data <= 8'h0;
        endcase
    end



    

    uart_tx inst_tx_uart (
        .clk                (clk),
        .rst_n              (rst_n),
        .din                (tx_data),
        .din_vld            (end_cnt_1s),
        .dout               (tx1)
    );




endmodule